package vue;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import controleur.ActionOkDialog;
import controleur.AleatoireVie;
import controleur.Dimension;
import controleur.DonnerVie;
import controleur.ModifierThread;
import controleur.PossedeVie;
import controleur.Tor;
import controleur.Vitesse;

import model.Environnement;
import model.Rules;
import model.ThreadEnvironnement;

public class PreferenceDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Environnement env = null;
	private ThreadEnvironnement t = null;
	private PanneauPrincipal panPrinc = null;

	public PreferenceDialog(JFrame parent, String title, boolean modal,
			Environnement env, ThreadEnvironnement t, PanneauPrincipal panPrinc) {
		super(parent, title, modal);
		this.env = env;
		this.t = t;
		this.panPrinc = panPrinc;
		this.setSize(550, 320);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.initComponent();
	}

	private void initComponent() {

		JPanel paneVitesse = new JPanel(new GridLayout(0, 2));
		paneVitesse.setBorder(BorderFactory
				.createTitledBorder("Vitesse du jeu"));

		JLabel labelValeur = new JLabel(t.getTempsPause() + " ms");

		JSlider slider = new JSlider(20, 5000, 20);
		slider.setValue((int) t.getTempsPause());
		slider.addChangeListener(new Vitesse(t, labelValeur));

		JLabel labelThread = new JLabel("Nombre de threads ");
		JFormattedTextField jtThread = new JFormattedTextField(new Integer(
				env.getNbThread()));
		jtThread.setPreferredSize(new java.awt.Dimension(100, 25));

		jtThread.addActionListener(new ModifierThread(env));
		
		paneVitesse.add(slider);
		paneVitesse.add(labelValeur);
		paneVitesse.add(labelThread);
		paneVitesse.add(jtThread);
		
		JPanel paneDimension = new JPanel(new GridLayout(0, 2));
		paneDimension.setBorder(BorderFactory
				.createTitledBorder("Dimension du jeu"));

		JLabel labelLargeur = new JLabel("Largeur ");

		JFormattedTextField jtLargeur = new JFormattedTextField(new Integer(
				env.getLargeur()));
		jtLargeur.setPreferredSize(new java.awt.Dimension(100, 25));

		jtLargeur.addActionListener(new Dimension(env, true, panPrinc));

		JLabel labelHauteur = new JLabel("Hauteur ");

		JFormattedTextField jtHauteur = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		jtHauteur.setText(Integer.toString(env.getHauteur()));
		jtHauteur.addActionListener(new Dimension(env, false, panPrinc));
		jtHauteur.setPreferredSize(new java.awt.Dimension(100, 25));

		paneDimension.add(labelLargeur);
		paneDimension.add(jtLargeur);
		paneDimension.add(labelHauteur);
		paneDimension.add(jtHauteur);

		/* Rules */
		JPanel paneRules = new JPanel(new GridLayout(0, 2));
		paneRules.setBorder(BorderFactory.createTitledBorder("Règles du jeu"));

		JLabel labelLife = new JLabel("          Vie ");
		JCheckBox checkLife = new JCheckBox();
		checkLife.setSelected(Rules.haveLife());
		checkLife.addActionListener(new PossedeVie());
		
		JLabel labelRandomLife = new JLabel("Vie aléatoire ");
		JCheckBox checkRandom = new JCheckBox();
		checkRandom.setSelected(Rules.randomLife());
		checkRandom.addActionListener(new AleatoireVie());

		JLabel labelMaxLife = new JLabel("  Vie maximum ");
		JFormattedTextField jMaxLife = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		jMaxLife.setText(Integer.toString(Rules.getMaxLife()));
		jMaxLife.setPreferredSize(new java.awt.Dimension(50, 25));

		JLabel labelRandomGiveLife = new JLabel("Donner la vie ");
		JCheckBox checkGiveLife = new JCheckBox();
		checkGiveLife.setSelected(Rules.giveLife());
		checkGiveLife.addActionListener(new DonnerVie());
		
		JLabel labelTor = new JLabel(" Activé Tor");
		JCheckBox checkTor = new JCheckBox();
		checkTor.setSelected(Rules.haveTor());
		checkTor.addActionListener(new Tor());
		
		paneRules.add(labelLife);
		paneRules.add(checkLife);
		paneRules.add(labelRandomLife);
		paneRules.add(checkRandom);
		paneRules.add(labelMaxLife);
		paneRules.add(jMaxLife);
		paneRules.add(labelRandomGiveLife);
		paneRules.add(checkGiveLife);
		paneRules.add(labelTor);
		paneRules.add(checkTor);

		JPanel pane = new JPanel();
		pane.add(paneVitesse, BorderLayout.NORTH);
		pane.add(paneDimension, BorderLayout.SOUTH);
		pane.add(paneRules, BorderLayout.CENTER);

		this.getContentPane().add(pane, BorderLayout.CENTER);
		JPanel paneControl = new JPanel();
		JButton okBouton = new JButton(new ActionOkDialog(this));
		paneControl.add(okBouton, BorderLayout.WEST);
		this.getContentPane().add(paneControl, BorderLayout.SOUTH);
	}
}
